<?php
class Info_model extends CI_Model {

    public function __construct()
    {
        $this->load->database();
    }

    public function check_prof($username)
    {

        $this->db->select ('*');
        $this->db->from   ('user_accounts');
        $this->db->where  ('user_id', $username);

        $query = $this->db->get();

        if($query->num_rows() == 1)
            return $query->row();
        else
            return false;

    }

    public function check_acc($username)
    {

        $this->db->select ('*');
        $this->db->from   ('user_profile');
        $this->db->where  ('user_id', $username);

        $query = $this->db->get();

        if($query->num_rows() == 1)
            return $query->row();
        else
            return false;

    }

    public function check_college($coll_id)
    {

        $this->db->select ('*');
        $this->db->from   ('user_col');
        $this->db->where  ('coll_id', $coll_id);

        $query = $this->db->get();

        if($query->num_rows() == 1)
            return $query->row();
        else
            return false;

    }

    public function check_pass($old)
    {

        $this->db->select ('*');
        $this->db->from   ('user_accounts');
        $this->db->where  ('password', $old);

        $query = $this->db->get();

        if($query->num_rows() == 1)
            return $query->row();
        else
            return false;

    }

    public function check_user($user)
    {

        $this->db->select ('*');
        $this->db->from   ('user_accounts');
        $this->db->join   ('user_type', 'user_type.type_no = user_accounts.type_no', 'left');
        $this->db->where  ('user_id', $user);

        $query = $this->db->get();

        return $query->row_array();

    }

    public function change_info($user, $new)
    {

        $this->db->where    ('user_id', $user);
        $this->db->update   ('user_profile', $new);

    }

    public function change_pass($user, $new)
    {

        $data_db = array('password' => $new);

        $this->db->where    ('user_id', $user);
        $this->db->update   ('user_accounts', $data_db);

    }

    public function check_image($username)
    {

        $this->db->select ('*');
        $this->db->from   ('user_img');
        $this->db->where  ('user_id', $username);

        $query = $this->db->get();

        if($query->num_rows() == 1)
            return $query->row();
        else
            return false;

    }

    public function change_image($user, $data)
    {

        $new = array(
            "img_name" => $data['file_name']
        );

        $this->db->select ('*');
        $this->db->from   ('user_img');
        $this->db->where  ('user_id', $user);

        $query = $this->db->get();
        $data = $query->row();

        if($data->img_name != "default.png")
            unlink("assets/images/profile/" . $data->img_name);

        $this->db->where    ('user_id', $user);
        $this->db->update   ('user_img', $new);

    }

}